﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
    BusinessLogicReference.BusinessLogicClient blClient = new BusinessLogicReference.BusinessLogicClient();
    BusinessLogicReference.CharacterInFo[] characters = null;
    BusinessLogicReference.GameNews[] gameNews = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        updateNews();
        updateCharacters();
    }
    protected void updateNews()
    {
        gameNews = blClient.GetNews();
        foreach(BusinessLogicReference.GameNews news in gameNews)
        {
            Panel newsItem = new Panel();
                newsItem.CssClass = "newsItem";
            Label headline = new Label();
                headline.CssClass = "newsHeadLine";
                headline.Text = news.Headline;
                newsItem.Controls.Add(headline);
            Label nDate = new Label();
                nDate.CssClass = "newsDate";
                nDate.Text = news.Date;
                newsItem.Controls.Add(nDate);
            Label details = new Label();
                details.CssClass = "newsDetails";
                details.Text = news.Details;
                newsItem.Controls.Add(details);
            NewsReel.Controls.Add(newsItem);
        }

    }
    protected void updateCharacters()
    {
        CharacterList.Controls.Clear();
        //  recreate the new character button
        Button newChar = new Button();
        newChar.Text = "Create New Character!";
        newChar.CssClass = "charButton";
        newChar.ID = "btnNewChar";
        newChar.Click += new EventHandler(btnNewChar_Click);
        CharacterList.Controls.Add(newChar);

        characters = blClient.GetCharacters(Session["UserID"].ToString());
        int charID = 0;

        foreach(BusinessLogicReference.CharacterInFo charecter in characters)
        {
            Button aButton = new Button();
            aButton.CssClass = "charButton";
            aButton.Text = charecter.CharName;
            aButton.ID = charID.ToString();
            aButton.Click += new EventHandler(characterButton);
            CharacterList.Controls.Add(aButton);
            charID += 1;

        }

    }
    protected void characterButton(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        //BusinessLogicReference.CharacterInFo myChar = (BusinessLogicReference.CharacterInFo)sender;
        //add code to populate character form with character Data
        int ID = Convert.ToInt32(btn.ID);
        txtName.Text = characters[ID].CharName;
        txtName.Enabled = false;
        charHP.Text = characters[ID].CharHP.ToString();
        charMP.Text = characters[ID].CharMP.ToString();
        charStr.Text = characters[ID].CharStr.ToString();
        charDex.Text = characters[ID].CharDex.ToString();
        charInt.Text = characters[ID].CharInt.ToString();
        txtDescription.Text = characters[ID].CharDescription;
        lblAvailable.Text = "0";
        CharID.Value = characters[ID].CharID.ToString();
        btnPlay.Enabled = true;
        CharacterDetails.Visible = true;
        Blackout.Visible = true;
    }
    protected void btnNewChar_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtName.Enabled = true;
        charHP.Text = "10";
        charMP.Text = "10";
        charStr.Text = "1";
        charDex.Text = "1";
        charInt.Text = "1";
        txtDescription.Text = "";
        lblAvailable.Text = "34";
        CharID.Value = "0";
        btnPlay.Enabled = false;
        CharacterDetails.Visible = true;
        Blackout.Visible = true;

    }
    protected void modifyStat(Label stat, int mod, bool HPorMP)
    {
        int availablePoints = Convert.ToInt32(lblAvailable.Text);
        int value = Convert.ToInt32(stat.Text);
        if ( availablePoints > 0 && mod > 0)
        {
            if (HPorMP)
            {
                value += 10;
                stat.Text = value.ToString();

            }
            else
            {
                value += 1;
                stat.Text = value.ToString();
            }
            availablePoints -= 1;
            lblAvailable.Text = availablePoints.ToString();
        }
        if (mod < 0 && value > 1 )
        {
            if (HPorMP && value > 10)
            {
                value -= 10;
                stat.Text = value.ToString();
                availablePoints += 1;

            }
            else if (!HPorMP)
            {
                value -= 1;
                stat.Text = value.ToString();
                availablePoints += 1;
            }
            lblAvailable.Text = availablePoints.ToString();

        }
    }
    protected void btnHPup_Click(object sender, EventArgs e)
    {
        modifyStat(charHP, 1, true);
    }
    protected void btnMPup_Click(object sender, EventArgs e)
    {
        modifyStat(charMP, 1, true);
    }
    protected void btnSTRup_Click(object sender, EventArgs e)
    {
        modifyStat(charStr, 1, false);
    }
    protected void btnDEXup_Click(object sender, EventArgs e)
    {
        modifyStat(charDex, 1, false);
    }
    protected void btnINTup_Click(object sender, EventArgs e)
    {
        modifyStat(charInt, 1, false);
    }
    protected void btnHPdown_Click(object sender, EventArgs e)
    {
        modifyStat(charHP, -1, true);
    }
    protected void btnMPdown_Click(object sender, EventArgs e)
    {
        modifyStat(charMP, -1, true);
    }
    protected void btnSTRdown_Click(object sender, EventArgs e)
    {
        modifyStat(charStr, -1, false);
    }
    protected void btnDEXdown_Click(object sender, EventArgs e)
    {
        modifyStat(charDex, -1, false);
    }
    protected void btnINTdown_Click(object sender, EventArgs e)
    {
        modifyStat(charInt, -1, false);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        BusinessLogicReference.CharacterInFo aChar = new BusinessLogicReference.CharacterInFo();

        aChar.UserID = Convert.ToInt32(Session["UserID"]);
        aChar.CharID = Convert.ToInt32(CharID.Value);
        aChar.CharName = txtName.Text;
        aChar.CharDescription = txtDescription.Text;

        aChar.CharHP = Convert.ToInt32(charHP.Text);
        aChar.CharMP = Convert.ToInt32(charMP.Text);
        aChar.CharStr = Convert.ToInt32(charStr.Text);
        aChar.CharDex = Convert.ToInt32(charDex.Text);
        aChar.CharInt = Convert.ToInt32(charInt.Text);

        Message.Text = blClient.SaveCharacter(aChar);
        updateCharacters();
        CharacterDetails.Visible = false;
        Blackout.Visible = false;
    }
    protected void btnPlay_Click(object sender, EventArgs e)
    {
        Session["CharacterID"] = CharID.Value;
        Response.Redirect("Console.aspx");
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        CharacterDetails.Visible = false;
        Blackout.Visible = false;
    }
}